package com.myblog.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myblog.Mapper.typemapper;
import com.myblog.po.Blog;
import com.myblog.po.Type;
import com.myblog.service.BlogService;
import com.myblog.vo.BlogQuery;
import com.myblog.vo.TypeQuery;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class TypeShowController {

    @Autowired
    private typemapper typeService;

    @Autowired
    private BlogService blogService;


    @GetMapping("types/{id}")
    /*@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable*/
    public String type(@RequestParam(value = "page",required = false) Integer page,
                       @PathVariable Long id,
                       Model model){
        if(page == null){
            page = 1;
        }
        List<Type> types = typeService.listtype();

        for(Type t :types){

            BlogQuery b  = new BlogQuery();
            b.setTypeid(t.getId());
            t.setBlogs(blogService.listBlogbyTypeid(b));

        }

        if(id == -1){
            id = types.get(0).getId();
        }

        PageHelper.startPage(page,8);

        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeid(id);
         List<Blog> blogs =  blogService.listBlogbyTypeid(blogQuery);
        for (Blog blog: blogs) {
            blog.setType(typeService.getType(blog.getType_id()));
        }
        PageInfo pages = new PageInfo(blogs);

        model.addAttribute("types",types);
        model.addAttribute("page",pages);
        model.addAttribute("activeTypeId",id);
        return "types";
    }


}
